package com.yang.newCustomersTransferBackProcess.service;

import com.yang.newCustomersTransferBackProcess.dao.CustomerCallDao;
import com.yang.newCustomersTransferBackProcess.dao.CustomerDao;
import com.yang.newCustomersTransferBackProcess.dao.MergeCustomerDao;
import com.yang.newCustomersTransferBackProcess.pojo.CustomerCall;
import com.yang.newCustomersTransferBackProcess.pojo.MergeCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MergeCustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private CustomerCallDao customerCallDao;

    @Autowired
    private MergeCustomerDao mergeCustomerDao;

    public Map<String, Object> mergeToExamine(int mergeCustomerId, String pass){

        MergeCustomer mergeCustomer = mergeCustomerDao.findMergeCustomer(mergeCustomerId);

        if (pass.equals("1")) {
            mergeCustomer.setStatus(2);
            mergeCustomerDao.save(mergeCustomer);

            Map<String, Object> msg = new HashMap<String, Object>();
            msg.put("massage", "审核成功");
            msg.put("code", 200);
            return msg;
        }else {
            mergeCustomer.setStatus(3);
            mergeCustomerDao.save(mergeCustomer);

            Map<String, Object> msg = new HashMap<String, Object>();
            msg.put("massage", "驳回成功");
            msg.put("code", 200);
            return msg;
        }
    }

    @Transactional
    public Map<String, Object> merge(int mergeCustomerId){

        Specification<MergeCustomer> mergeSpecification = new Specification<MergeCustomer>() {

            @Override
            public Predicate toPredicate(Root<MergeCustomer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, mergeCustomerId);

                return criteriaBuilder.and(predicateId);
            }
        };
        MergeCustomer mergeCustomer = mergeCustomerDao.findOne(mergeSpecification).get();

        Specification<CustomerCall> callSpecification = new Specification<CustomerCall>() {

            @Override
            public Predicate toPredicate(Root<CustomerCall> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("customerId");
                Predicate predicateId = criteriaBuilder.equal(id, mergeCustomer.getNeedConsolidateCustomerId());

                return criteriaBuilder.and(predicateId);
            }
        };
        List<CustomerCall> customerCallList = customerCallDao.findAll(callSpecification);

        for (CustomerCall c:customerCallList
             ) {
            c.setCustomerId(mergeCustomer.getKeepCustomerId());
            c.setCustomerName(mergeCustomer.getKeepCustomerName());
            customerCallDao.save(c);
        }

        customerDao.deleteById(mergeCustomer.getNeedConsolidateCustomerId());

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "合并成功");
        msg.put("code", 200);
        return msg;
    }

    public List<MergeCustomer> queryMergeCustomer(){

       List<MergeCustomer> mergeCustomerList = mergeCustomerDao.findAll();

       return mergeCustomerList;
    }

    public Map<String, Object> toApplyFor(
             int needConsolidateCustomerId,
             String needConsolidateCustomername,
             int keepCustomerId,
             String keepCustomerName,
             String reason){

        MergeCustomer mergeCustomer = new MergeCustomer();
        mergeCustomer.setNeedConsolidateCustomerId(needConsolidateCustomerId);
        mergeCustomer.setNeedConsolidateCustomername(needConsolidateCustomername);
        mergeCustomer.setKeepCustomerId(keepCustomerId);
        mergeCustomer.setKeepCustomerName(keepCustomerName);
        mergeCustomer.setStatus(1);
        mergeCustomer.setReason(reason);
        mergeCustomerDao.save(mergeCustomer);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "申请成功");
        msg.put("code", 200);
        return msg;
    }

}
